package com.depot.fastReport.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.depot.fastReport.dao.FastReportDao;
import com.soar.context.Environment;
import com.soar.util.webfastreport.BaseReportAction;
import com.soar.util.webfastreport.ReportColumn;
import com.soar.util.webfastreport.ReportDataSet;
import com.soar.util.webfastreport.ReportObject;

/**
 * 
 * @author zhangaoxiang
 * @version $Revision$ - $Author$ at $Date$
 * @description com.depot.fastReport.web.FastReportAction
 */
public class FastReportAction extends BaseReportAction
{
	private static final long serialVersionUID = 1L;
	
	public HashMap<String,?> recordHash = new HashMap<String, String>();
	
	public HashMap<String,?> contactHash = new HashMap<String, String>();
	
	public List<HashMap<String, String>> goodsList = new ArrayList<HashMap<String,String>>();
	
	public List<HashMap<String,String>> boxstList = new ArrayList<HashMap<String,String>>();
	
	public List<HashMap<String,String>> cboxstList = new ArrayList<HashMap<String,String>>();
	
	/**
	 * 打印查验记录单
	 * @param env
	 * @throws Exception
	 */
	public void printRecordBill(Environment env) throws Exception
	{
		String guId = env.getRequest().getParameter("ENTRY_ID");
		if (guId != null)
		{
			this.recordHash = FastReportDao.selectRecordInfoByEntryId(env, guId);//记录单信息
			//this.boxstList = FastReportDao.selectConpays(env, guId);//集装箱信息
			this.goodsList = FastReportDao.selectRecordGoods(env, guId);//货物信息
			List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();//组合货物信息
			if(goodsList.size()>0)
			{
				for (int i = 0; i < goodsList.size(); i++)
				{
					HashMap<String,String> map = new HashMap<String, String>();
					map.put("G_NO", goodsList.get(i).get("G_NO").toString());
					map.put("CODE_TS", goodsList.get(i).get("CODE_TS").toString());
					map.put("G_NAME_MODEL", goodsList.get(i).get("G_NAME").toString()+"/"+goodsList.get(i).get("G_MODEL").toString());
					map.put("G_QTY_UNIT", goodsList.get(i).get("G_QTY").toString()+goodsList.get(i).get("G_UNIT").toString());
					map.put("ORIGIN_COUNTRY", goodsList.get(i).get("ORIGIN_COUNTRY").toString());
					map.put("DECL_TOTAL_CURR", goodsList.get(i).get("DECL_TOTAL").toString()+"/"+goodsList.get(i).get("TRADE_CURR").toString());
					list.add(map);
				}
			}
			// 循环轮空
			HashMap<String, String> product = new HashMap<String, String>();
			product.put("G_NO", "");
			product.put("CODE_TS", "");
			product.put("G_NAME_MODEL","" );
			product.put("G_QTY_UNIT", "");
			product.put("ORIGIN_COUNTRY", "");
			product.put("DECL_TOTAL_CURR", "");
			while (list.size() < 4)
			{
				list.add(product);
			}
			
			HashMap<String, String> product1 = new HashMap<String, String>();
			product.put("G_NO1", "");
			product.put("CODE_TS1", "");
			product.put("G_NAME_MODEL1","" );
			product.put("G_QTY_UNIT1", "");
			product.put("ORIGIN_COUNTRY1", "");
			product.put("DECL_TOTAL_CURR1", "");
			while (boxstList.size() < 4)
			{
				boxstList.add(product1);
			}

			ReportObject report = new ReportObject(this.getOnlyData(), this.getReportName(), "report/");
			report.setCharsetName("UTF-8");
			//记录单信息
			report.addVariable("ENTRY_ID",guId);//报关单编号
			report.addVariable("PRE_ENTRY_ID", (String) this.recordHash.get("PRE_ENTRY_ID"));//查验记录单编号
			report.addVariable("TRADE_NAME", (String) this.recordHash.get("TRADE_NAME"));//经营单位
			report.addVariable("TRAF_NAME", (String) this.recordHash.get("TRAF_NAME"));//运输工具名称
			report.addVariable("D_DATE", (String) this.recordHash.get("F_D_DATE"));//申报日期
			report.addVariable("I_E_DATE", (String) this.recordHash.get("F_I_E_DATE"));//进出口日期
			report.addVariable("OWNER_NAME", (String) this.recordHash.get("OWNER_NAME"));//收发货单位
			report.addVariable("BILL_NO", (String) this.recordHash.get("BILL_NO"));//提运单号
			if(this.recordHash.get("TRADE_MODE").equals("0110"))
			{
				report.addVariable("TRADE_MODE","一般贸易");//监管方式
			}
			else
			{
				report.addVariable("TRADE_MODE", (String) this.recordHash.get("TRADE_MODE"));
			}
			report.addVariable("TRAF_MODE", (String) this.recordHash.get("TRAF_MODE"));//运输方式
			report.addVariable("AGENT_NAME", (String) this.recordHash.get("AGENT_NAME"));//申报单位
			report.addVariable("PACK_NO", (String) this.recordHash.get("PACK_NO"));//件数
			report.addVariable("WRAP_TYPE", (String) this.recordHash.get("WRAP_TYPE"));//包装种类
			report.addVariable("GROSS_WT", (String) this.recordHash.get("GROSS_WT"));//毛重
			report.addVariable("NET_WT", (String) this.recordHash.get("NET_WT"));//净重
			report.addVariable("NOTE_S", (String) this.recordHash.get("NOTE_S"));//备注

			List<ReportColumn> metaData1 = new ArrayList<ReportColumn>();
			metaData1.add(new ReportColumn("G_NO", null, "String", 100));
			metaData1.add(new ReportColumn("CODE_TS", null, "String", 100));
			metaData1.add(new ReportColumn("G_NAME_MODEL", null, "String", 100));
			metaData1.add(new ReportColumn("G_QTY_UNIT", null, "String", 100));
			metaData1.add(new ReportColumn("ORIGIN_COUNTRY", null, "String", 100));
			metaData1.add(new ReportColumn("DECL_TOTAL_CURR", null, "String", 100));

			ReportDataSet dataset1 = new ReportDataSet("goodsList", metaData1, list);
			ReportDataSet dataset2 = new ReportDataSet("goodsList1", metaData1, list);
			ReportDataSet dataset3 = new ReportDataSet("boxsList", metaData1, boxstList);
			ReportDataSet dataset4 = new ReportDataSet("boxsList1", metaData1, boxstList);
			if (dataset1.getList().size() > 0)
			{
				report.addDataSet(dataset1);
			}
			if (dataset2.getList().size() > 0)
			{
				report.addDataSet(dataset2);
			}
			if (dataset3.getList().size() > 0)
			{
				report.addDataSet(dataset3);
			}
			if (dataset4.getList().size() > 0)
			{
				report.addDataSet(dataset4);
			}
			env.getResponse().setCharacterEncoding("UTF-8");
			env.getResponse().getOutputStream().print(report.build());
		}
	}
	
	/**
	 * 打印查验联系单
	 * @param env
	 * @throws Exception
	 */
	public void printContactBill(Environment env) throws Exception
	{
		String guId = env.getRequest().getParameter("GUID");
		if (guId != null)
		{
			this.contactHash = FastReportDao.selectContactInfoByGuid(env, guId);//查验单信息
			this.cboxstList = FastReportDao.selectContactBoxs(env, this.contactHash.get("CHECK_BILL_NO").toString());//查验集装箱信息
			// 循环轮空
			HashMap<String, String> product = new HashMap<String, String>();
			product.put("CONTAINER_ID", "");
			product.put("SEAL_ID", "");
			product.put("CONTAINER_WT","" );
			product.put("CONTAINER_SIZE", "");
			while (cboxstList.size() < 12)
			{
				cboxstList.add(product);
			}

			ReportObject report = new ReportObject(this.getOnlyData(), this.getReportName(), "report/");
			report.setCharsetName("UTF-8");
			//联系单信息
			report.addVariable("ENTRY_ID",(String) this.contactHash.get("ENTRY_ID"));//报关单号
			report.addVariable("CHECK_PLACE_NAME",(String) this.contactHash.get("AGENT_NAME"));//查验地点
			report.addVariable("TRAF_NAME", (String) this.contactHash.get("TRAF_NAME"));//船名
			report.addVariable("VOYAGE_NO", (String) this.contactHash.get("VOYAGE_NO"));//航次
			report.addVariable("BILL_NO", (String) this.contactHash.get("BILL_NO"));//提运单号
			report.addVariable("AGENT_NAME", (String) this.contactHash.get("AGENT_NAME"));//申报人
			report.addVariable("EXAM_LINK_PERSON", (String) this.contactHash.get("EXAM_LINK_PERSON"));//报关员/查验联系人
			report.addVariable("EXAM_LINK_TEL", (String) this.contactHash.get("EXAM_LINK_TEL"));//报关员/查验联系人电话

			List<ReportColumn> metaData1 = new ArrayList<ReportColumn>();
			metaData1.add(new ReportColumn("CONTAINER_ID", null, "String", 100));
			metaData1.add(new ReportColumn("SEAL_ID", null, "String", 100));
			metaData1.add(new ReportColumn("CONTAINER_WT", null, "String", 100));
			metaData1.add(new ReportColumn("CONTAINER_SIZE", null, "String", 100));

			ReportDataSet dataset1 = new ReportDataSet("goodsList", metaData1, cboxstList);
			ReportDataSet dataset2 = new ReportDataSet("goodsList1", metaData1, cboxstList);
			if (dataset1.getList().size() > 0)
			{
				report.addDataSet(dataset1);
			}
			if (dataset2.getList().size() > 0)
			{
				report.addDataSet(dataset2);
			}
			env.getResponse().setCharacterEncoding("UTF-8");
			env.getResponse().getOutputStream().print(report.build());
		}
	}
}
